<template>
  <div>
    <!-- 设备备件到期提醒记录批量修改备注弹窗 -->
    <el-dialog
      :close-on-click-modal="false"
      v-model="visibleDialog"
      :title="props.title"
      width="500px"
      @close="closeDialog"
    >
      <el-form
        ref="formRef"
        :model="formData"
        :rules="rules"
        label-width="80px"
        label-position="left"
      >
        <el-form-item label="提醒内容" prop="reminderDesc">
          <el-input
            type="textarea"
            v-if="!props.isReadonly"
            v-model="formData.reminderDesc"
          />
        </el-form-item>
      </el-form>

      <template #footer>
        <div class="dialog-footer">
          <el-button
            v-if="!props.isReadonly"
            type="primary"
            :loading="loading"
            @click="handleSubmit"
            >更新</el-button
          >
          <el-button @click="closeDialog">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { getMdminspectiontoolApi } from "@/utils/commonData";
defineOptions({
  name: "ESPDUEREMINDERUPDATE",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
    if (newVal == true) {
      resetForm();
    }
    setTimeout(() => {
      formRef.value.clearValidate();
    }, 50);
  }
);

const formRef = ref(ElForm);

let formData = reactive({
  reminderLife: "",
  reminderName: "",
  reminderDesc: "",
  id: "",
  remarks: "",
  createTime: "",
  createdUserId: "",
  createdUserName: "",
  lastUpdateTime: "",
  lastUpdatedUserId: "",
  lastUpdatedUserName: "",
  enabled: "",
  deleted: "",
});

watch(
  () => props.data,
  (newVal: any) => {
    formData.id = "";
    if (newVal?.id) {
      const {
        reminderLife,
        reminderName,
        reminderDesc,
        id,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        enabled,
        deleted,
        reminderType,
      } = newVal;
      Object.assign(formData, {
        reminderLife,
        reminderName,
        reminderDesc,
        id,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        enabled,
        deleted,
        reminderType,
      });
    }
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  resetForm();
  emit("change-visible", false);
  visibleDialog.value = false;
}

/** 重置表单 */
function resetForm() {
  formData.reminderDesc = "";
}

const loading = ref(false);

/** 产品工装关系表保存提交 */
async function handleSubmit() {
  const res = await request({
    url: `/eam/eammouldlifeconfig/save`,
    method: "post",
    data: formData,
  });
  closeDialog();
  emit("reset-table", formData.id);
  ElMessage.success(`更新成功`);
}

// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});

onMounted(() => {});

const rules = reactive({});
</script>
<style scoped lang="scss">
.el-input {
  width: 80%;
}
</style>
